package com.gukewen.aihaiguitang.util;

import cn.hutool.core.collection.CollUtil;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionChoice;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;



/*
 * AI 调用工具类
 * @author gukewen
 * @date 2025/4/24
 */
@Component
public class AiUtil {

    @Resource
    private ArkService arkService;

    /**
     * 这个方法只允许传入一个系统预设和用户预设，不能关联上下文
     * @param systemPrompt
     * @param userPrompt
     * @return
     */
    public String dochat(String systemPrompt, String userPrompt){
        final List<ChatMessage> messages = new ArrayList<>();
        final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content(systemPrompt).build();
        final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content(userPrompt).build();
        messages.add(systemMessage);
        messages.add(userMessage);

        return dochat(messages);
    }


    /**
     * 更加通用
     * 这个方法可以带上之前的会话记录，可以关联上下文
     * @return
     */
    public String dochat(List<ChatMessage> messagesList){

        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                // 指定您创建的方舟推理接入点 ID
                .model("deepseek-v3-250324")
                .messages(messagesList)
                .build();

        List<ChatCompletionChoice> choices = arkService.createChatCompletion(chatCompletionRequest).getChoices();
        if (CollUtil.isEmpty(choices)){
            new RuntimeException("no choices");
        }
        String content = (String) choices.get(0).getMessage().getContent();
        System.out.println(" Ai 返回的内容：" + content);
        return content;
    }
}
